"""
Description: 提取39百科有层级的页面，无层级的页面保存达到log2中，使用extract_structured_data2脚本处理
"""
import json

from lxml import etree

from utils import get_disease_url, split_lines, extractor

from log import my_log

logger = my_log('disease')

"""
疾病概述
临床表现与病因 （症状 疾病分类 病因 病理生理）
检查
诊断
治疗
护理
并发症
"""


class Disease:
    """
    疾病路径索引
    """
    HOME = '/home.html'
    INTRO = '/jbzs.html'
    SYMPTOM = '/zztz.html'  # 症状和原因结合在一起，症状中如果出现 诊断 需要将其放到 （检查与诊断下）
    REASON = '/blby.html'
    EXAM = '/jcjb.html'  # 检查 诊断 鉴别 一起
    IDENTIFICATION = '/jb.html'
    TREATMENT = '/yyzl.html'  # 治疗
    PREVENTION = '/yfhl.html'  # 预防护理和饮食保健
    NURSING = '/hl.html'
    DIET = '/ysbj.html'
    COMPLICATION = '/bfbz.html'


def read_content(path):
    """
    读取html页面的内容
    :param path:
    :return: xpath对象
    """
    try:
        with open(path, mode='r', encoding='gb18030') as f:
            content = f.read()
            return etree.HTML(content)
    except FileNotFoundError:
        logger.exception(path + '下文件未找到', exc_info=True)
        return None


def make_json_tree(disease_data_path, disease_index, disease_name):
    """
    根据疾病路径，找到其对应的页面，获得xpath_tree，并将xpath_tree转成json_tree返回
    :param disease_data_path:
    :param disease_index:
    :param disease_name:
    :return:
    """
    path = disease_data_path + disease_name + disease_index
    xpath_tree = read_content(path)

    # 如果是饮食页面则需要特殊处理
    if disease_index == '/ysbj.html':
        diet_list = xpath_tree.xpath('//div[@class="yinshi_title"]/text()')
        # 饮食可能有多个：所以就切割一次 提取标题就好
        diet_list2 = [{'title': item.split('：', 1)[0], 'text': item.split('：', 1)[1]} for item in diet_list]
        if diet_list2:
            return [{'title': '饮食', 'content': diet_list2}]
        return extractor.extract_from_origin_page(xpath_tree)
    # 疾病简介页面也需要特殊处理
    elif disease_index == '/jbzs.html':
        home_path = disease_data_path + disease_name + Disease.HOME
        home_tree = read_content(home_path)
        json_tree = extractor.extract_from_intro_page(xpath_tree, home_tree)
    elif disease_index == '/zztz.html':
        json_tree = extractor.extract_from_symptom_page(xpath_tree, disease_name)

    # 鉴别 预防 检查   dt在外
    elif disease_index == '/jcjb.html' or disease_index == '/yfhl.html' or disease_index == '/jb.html':
        json_tree = extractor.extract_from_special_page(xpath_tree)

    # 治疗 护理  原因 并发症
    else:
        json_tree = extractor.extract_from_origin_page(xpath_tree)

    return json_tree


if __name__ == '__main__':
    disease_url_path = 'D:/PycharmProjects/39_health_disease/disease_fuse_no_dup.txt'
    disease_data_path = 'D:/PycharmProjects/39html_data/disease/'
    file_data_path = 'D:/PycharmProjects/39html_data/json_data/'
    i = 0
    for i in range(1176, len(get_disease_url(disease_url_path))):

        print(i + 1)
        disease_name = split_lines(get_disease_url(disease_url_path)[i])
        # disease_name = 'ajjnml'
        # try:
        # 获取疾病中文名
        home_path = disease_data_path + disease_name + Disease.HOME
        home_tree = read_content(home_path)
        name = home_tree.xpath('//h1/text()')[0]
        all_dict = {'title': name, 'content': []}

        dict1 = {'title': '疾病简介', 'content': []}
        json_intro_tree: dict = make_json_tree(disease_data_path, Disease.INTRO, disease_name)
        dict1['content'].extend(json_intro_tree)
        all_dict['content'].append(dict1)

        # 典型症状页面
        json_symptom_tree = make_json_tree(disease_data_path, Disease.SYMPTOM, disease_name)
        if json_symptom_tree is None:
            continue

        # 发病原因页面
        json_reason_tree = make_json_tree(disease_data_path, Disease.REASON, disease_name)

        # 检查页面
        json_exam_tree = make_json_tree(disease_data_path, Disease.EXAM, disease_name)

        # 鉴别页面
        json_identification_tree = make_json_tree(disease_data_path, Disease.IDENTIFICATION, disease_name)

        # 治疗页面
        json_treatment_tree = make_json_tree(disease_data_path, Disease.TREATMENT, disease_name)

        # 预防页面
        json_prevention_tree = make_json_tree(disease_data_path, Disease.PREVENTION, disease_name)
        # json_prevention_tree2 = [{'title:': '预防', 'content': json_prevention_tree}]

        # 护理页面
        json_nursing_tree = make_json_tree(disease_data_path, Disease.NURSING, disease_name)

        # 饮食页面
        json_diet_tree = make_json_tree(disease_data_path, Disease.DIET, disease_name)

        # 并发症页面
        json_complication_tree = make_json_tree(disease_data_path, Disease.COMPLICATION, disease_name)
        # json_complication_tree2 = [{'title': '并发症', 'content': json_complication_tree}]

        all_dict['content'].extend(
            json_symptom_tree + json_reason_tree + json_exam_tree + json_identification_tree + json_treatment_tree + json_prevention_tree + json_nursing_tree + json_diet_tree + json_complication_tree)

        json.dump(all_dict,
                  open(file_data_path + disease_name + '.json', mode='w', encoding='utf-8'),
                  ensure_ascii=False)
